#include <v8.h>

#include <syslog.h>

#include "counterpartExceptions.h"

using namespace v8;
using namespace std;

void Exceptions::ScheduleJavaScriptExceptionFunc (const exception * e, int line, const char * file, const char * func)
{
	syslog (LOG_ERR, "C++ function %s scheduling JavaScript exception from line %i file \"%s\"", func, line, file);

	if (!e)
	{
		(void) ThrowException (Exception::Error (String::New ("unknown C++ exception")));
		return;
	}

	const char * eWhat (e->what ( ));
	syslog (LOG_ERR, "transforming exception from C++ to JavaScript: %s", eWhat);
	size_t eLen (strlen (eWhat));
	(void) ThrowException (Exception::Error (String::New (eWhat, eLen)));
}

void Exceptions::LogThrow (const char * func, const char * file, int line)
{
	syslog (LOG_ERR, "function %s throwing C++ exception from line %i of file %s", func, line, file);
}

void Exceptions::DemandUmnet (const char * expr, const char * func, const char * file, int line)
{
	syslog (LOG_ERR, "C++ function %s found a demand unmet on line %i of file %s", func, line, file);
	FormatString message ("ummet: '%s' in function '%s' on line %i of file '%s'", expr, func, line, file);
	Throw (ScopedMessage ("demand", message.c_str ( )));
}
